package cn.lixiaodan.hqlbuilder.gen;

import cn.lixiaodan.hqlbuilder.model.ClassModel;

public class HqlGenerator implements IGenerator {
	public String make(ClassModel[] cmarr, String[] conditions) {
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT  \n");
		// 查询字段
		for (int i = 0; i != cmarr.length; i++) {
			ClassModel cm = cmarr[i];
			sql.append(i == 0 ? "" : ",\n");
			sql.append("\t/**").append(cm.getShortName()).append("*/");
			for (int j = 0; j != cm.getFields().size(); j++) {
				//字段名
				String field = cm.getFields().get(j);
				sql.append(j == 0 ? "\n" : ",\n").append("\t");
				sql.append("tb").append(i).append(".").append(field).append(" as ").append(field);
			}
		}
		// from
		sql.append("\n FROM \n");
		sql.append("\t");
		for (int i = 0; i != cmarr.length; i++) {
			ClassModel cm = cmarr[i];
			sql.append(i == 0 ? "" : ",");
			sql.append(cm.getShortName()).append(" tb").append(i);
		}
		// where
		if (conditions != null && conditions.length != 0) {
			sql.append("\n WHERE \n");
			for (int i = 0; i != conditions.length; i++) {
				sql.append(i == 0 ? "\t" : " AND \n\t");
				sql.append(conditions[i]).append("\n");
			}
		}
		return sql.toString();
	}
}
